package mo.tags.stack;

import java.util.Stack;

public class LO2_36 {

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if (token.equals("+")) {
                int v2 = stack.pop();
                int v1 = stack.pop();
                stack.push(v1+v2);
            } else if (token.equals("-")) {
                int v2 = stack.pop();
                int v1 = stack.pop();
                stack.push(v1-v2);
            } else if (token.equals("*")) {
                int v2 = stack.pop();
                int v1 = stack.pop();
                stack.push(v1*v2);
            } else if (token.equals("/")) {
                int v2 = stack.pop();
                int v1 = stack.pop();
                stack.push(v1/v2);
            } else {
                stack.push(Integer.parseInt(token));
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        LO2_36 test = new LO2_36();
        System.out.println(test.evalRPN(new String[]{"10","6","9","3","+","-11","*","/","*","17","+","5","+"}));
    }
}
